
clear all
set more off

use "MPPEC Dataset.dta"

gen id = _n

preserve

bysort pair: gen diff=citizens_speaking[2]-citizens_speaking[1] if _n==1

do hodges.do

restore 

merge 1:1 id using treatment_comb



*** Fisher's randomization experiment

*Keele et al - subtract k from all treatment outcomes of interest for various k

*********************************************
*DIFFERENCE IN MEANS
*********************************************
foreach num of numlist -10(0.5)85  {
preserve
replace citizens_speaking = citizens_speaking - `num'   if mobilization == 1
	sort pair mobilization
	capture drop diff
	bysort pair: gen diff=citizens_speaking[2]-citizens_speaking[1] if _n==1
	egen avg_diff_0=mean(diff) 
	drop diff

	
	
**************************************************************************
***                     Within pairs re-assignment of treatment
**************************************************************************

*preserve


local nsim=128                                                   // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************
	
	sort pair mobilization_`i'
	bysort pair: gen diff_`i'=citizens_speaking[2]-citizens_speaking[1] if _n==1
	egen avg_diff_`i'=mean(diff_`i') 
	drop diff_`i' mobilization_`i'

}
***
qui {
	***    Manipulating the output from the simulations
	**************************************************************************
	
	drop if _n>1
	keep avg_diff_*
	gen id=_n-1
	
	reshape long avg_diff_, i(id) j(bsrep)
	ren avg_diff_ avg_diff_sim
	drop id
	gen avg_diff=avg_diff_sim[1]
	drop if bsrep==0
	
	sort avg_diff_sim
	local sign=sign(avg_diff[1])
	noi di `num'
	sum avg_diff_sim
	local sim = r(mean)
	sum avg_diff
	local actual = r(mean)
	if `actual'> `sim' {
		
		qui count if avg_diff_sim>avg_diff
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`greater'/_N'
		
}
	
	else {

		qui count if avg_diff_sim<avg_diff
		local smaller=r(N)
		
		
		
		*** P-value calculation for one-tailed hypothesis tests
			
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`smaller'/_N'
		
		}
	}

restore

}
************************************************
*RANK SUM
************************************************

clear all
set more off

cd "C:\Users\Aletheia\Dropbox\Ryan\Kenya Local Gov Experiment\Data Archive"

use "Kenya Local Gov-August.dta"

gen id = _n

merge 1:1 id using treatment_comb
assert _merge == 3
drop _merge

*** Creating Wilcoxon Signed Rank test statistic for actual data
*foreach num of numlist -10(0.5)85  {
*preserve
*replace citizens_speaking = citizens_speaking -`num'  if mobilization == 1
sort pair mobilization
bysort pair: gen diff=citizens_speaking[2]-citizens_speaking[1] if _n==1
gen abs_diff = abs(diff)
egen rank = rank(abs_diff) if abs_diff != . & abs_diff != 0
egen w_0 = sum(rank) if diff > 0
sum w_0
replace w_0 = r(mean) if w_0 == .
drop diff-rank
	
**************************************************************************
***                     Within pairs re-assignment of treatment
**************************************************************************

* Creating Wilcoxon Signed Rank test statistic for simulated data

local nsim=128 
                                                 // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************
	
	sort pair mobilization_`i'
	bysort pair: gen diff_`i' =citizens_speaking[2]-citizens_speaking[1] if _n==1
	gen abs_diff_`i' = abs(diff_`i')
	egen rank_`i' = rank(abs_diff_`i') if abs_diff_`i' != . & abs_diff_`i' != 0
	egen w_`i' = sum(rank_`i') if diff_`i' > 0
	sum w_`i'
	replace w_`i' = r(mean) if w_`i' == .

}

*qui {
drop if _n>1
	keep w_*
	gen id=_n-1
	
	reshape long w_, i(id) j(bsrep)
	ren w_ w_sim
	drop id
	gen w=w_sim[1]
	drop if bsrep==0
	*drop if w_sim == .
	sort w_sim
	
	gen z_sim = (w_sim - 14)/5.916
	gen z = (w - 14)/5.916
	sort z_sim
	local sign=sign(z)
	noi di `num'
	sum w_sim
	local sim = r(mean)
	sum w
	local actual = r(mean)
	if `actual'> `sim' {
		
		qui count if w_sim>w
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`greater'/_N'
		
}
	
	else {

		qui count if w_sim<w
		local smaller=r(N)
		
		
		
		*** P-value calculation for one-tailed hypothesis tests
			
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`smaller'/_N'
		
		}

restore
}

